﻿using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Resources;
using System.IO;

namespace ComicReader
{
    public partial class MainForm : Form
    {
        private bool isButtonClicked = false;
        private double X, Y;
        private Image backup;
        private Cursor bkpCursor;
        private ComicFile comic;
        private FormState formState = new FormState();

        public MainForm()
        {
            InitializeComponent();
            backup = pictureBox.Image;
            this.pictureBox.Cursor = new Cursor(new MemoryStream(ComicReader.Properties.Resources.cursor));
            SetScrollBarValues();
        }

        #region Menu Events
        private void MainForm_Resize(object sender, EventArgs e)
        {
            this.ResizeImage();
        }


        private void pictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.Cursor = bkpCursor;
        }

        private void loadFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.openFileDialog.ShowDialog();
                this.comic = new ComicFile(this.openFileDialog.FileName);
                this.LoadImage(this.comic.Image);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.GetType() + " : " + ex.Message);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void firstPageToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void lastPageEndToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void nextPagePageDownToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void previousPagePageUpToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void configureToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tutorialToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isButtonClicked = !isButtonClicked;
                X = e.X;
                Y = e.Y;
            }
        }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isButtonClicked = !isButtonClicked;
                int val = this.vScrollBar.Value + Convert.ToInt32((e.Y - Y)*-1);
                if ((val)<(vScrollBar.Minimum))
                    val = vScrollBar.Minimum;
                if ((val)>(vScrollBar.Maximum))
                    val = vScrollBar.Maximum;
                this.vScrollBar.Value = val;
            }
        }

        private void vScrollBar_ValueChanged(object sender, EventArgs e)
        {
            this.MoveImage(this.vScrollBar.Value);
        }


        private void MainForm_KeyPress(object sender, KeyEventArgs e)
        {
            int val = 0;
            switch (e.KeyCode)
            {
                case Keys.Up:
                    val = vScrollBar.Value - Convert.ToInt32(vScrollBar.Size.Height * 0.1);
                    if ((val) < (vScrollBar.Minimum))
                        val = vScrollBar.Minimum;
                    vScrollBar.Value = val;
                    break;
                case Keys.Down:
                    val = vScrollBar.Value + Convert.ToInt32(vScrollBar.Size.Height * 0.1);
                    if ((val) > (vScrollBar.Maximum))
                        val = vScrollBar.Maximum;
                    vScrollBar.Value = val;
                    break;
                case Keys.PageDown:
                    LoadImage(this.comic.Next);
                    this.Y = 0;
                    this.vScrollBar.Value = 0;
                    break;
                case Keys.PageUp:
                    LoadImage(this.comic.Before);
                    this.Y = 0;
                    this.vScrollBar.Value = 0;
                    break;
                default:
                    Console.WriteLine("Key pressed: {0}", e.KeyCode);
                    break;
            }
        }
        #endregion

        private void MoveImage(int X)
        {
            if (this.pictureBox.Width * this.pictureBox.Image.Height / this.pictureBox.Image.Width < this.pictureBox.Height)
            {
                this.ResizeImage();
                return;
            }
            Graphics g = this.pictureBox.CreateGraphics();
            Rectangle destRect = new Rectangle(0, 0, this.pictureBox.Width, this.pictureBox.Width * this.pictureBox.Image.Height / this.pictureBox.Image.Width);
            Rectangle srcRect = new Rectangle(0, X, this.pictureBox.Image.Width, this.pictureBox.Image.Height);
            g.DrawImage(pictureBox.Image, destRect, srcRect, GraphicsUnit.Pixel);
            pictureBox.Update();
        }

        private void LoadImage(Image img)
        {
            bkpCursor = this.Cursor;
            this.Cursor = Cursors.WaitCursor;
            if (img == null) throw new Exception("Could not load image.");
            this.backup = img;
            this.ResizeImage();
            this.Cursor = bkpCursor;
        }

        private Image Redraw(int width, int height)
        {
            Image resized = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage(resized))
            {
                Rectangle srceRect = new Rectangle(0, 0, backup.Width, backup.Height);
                Rectangle destRect = new Rectangle(0, 0, resized.Width, resized.Height);

                g.DrawImage(backup, destRect, srceRect, GraphicsUnit.Pixel);
            }
            return resized;
        }

        private void ResizeImage()
        {
            Graphics g = this.pictureBox.CreateGraphics();
            this.pictureBox.Image.Dispose();
            this.pictureBox.Image = Redraw(this.pictureBox.Width, this.pictureBox.Width * this.backup.Height / this.backup.Width);
            SetScrollBarValues();
            pictureBox.Update();
        }

        public void SetScrollBarValues()
        {
            this.vScrollBar.Minimum = 0;
            int size;
            if ((size = this.pictureBox.Image.Height - this.pictureBox.Height) < 0)
                size = 0;
            this.vScrollBar.Maximum = size;
         
            this.vScrollBar.LargeChange = Convert.ToInt32(this.vScrollBar.Maximum * 0.2);
            this.vScrollBar.SmallChange = Convert.ToInt32(this.vScrollBar.Maximum * 0.1);
        }

        //
        //formState.Maximize(this);
        //this.ResizeImage();
        //
        //rotate - picturebox1.Image.RotateFlip((RotateFlipType.Rotate270FlipXY);
    }
}
